/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.xy.biz.base.client.DictClient;
import com.xy.biz.rs.domain.bo.ManufactureRecordBO;
import com.xy.biz.rs.domain.constants.ManufactureRecordConstants;
import com.xy.biz.rs.domain.constants.ManufactureRecordModifyApplyConstants;
import com.xy.biz.rs.manager.ManufactureRecordManager;
import com.xy.biz.uc.client.UserClient;
import com.xy.tool.context.UserContextHolder;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.rs.domain.bo.ManufactureRecordModifyApplyBO;
import com.xy.biz.rs.domain.query.ManufactureRecordModifyApplyQuery;
import com.xy.biz.rs.service.ManufactureRecordModifyApplyService;
import com.xy.biz.rs.manager.ManufactureRecordModifyApplyManager;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 *
 * @author wwj
 * @date 2021-4-16
 */
@Service
public class ManufactureRecordModifyApplyServiceImpl implements ManufactureRecordModifyApplyService {

	@Autowired
	private ManufactureRecordModifyApplyManager manufactureRecordModifyApplyManager;
	@Autowired
	private ManufactureRecordManager manufactureRecordManager;
	@Autowired
	private DictClient dictClient;
	@Autowired
	private UserClient userClient;

	@Transactional
	@Override
	public Result<?> saveManufactureRecordModifyApply(ManufactureRecordModifyApplyBO manufactureRecordModifyApplyBO) {
		buildBO(manufactureRecordModifyApplyBO);
		int affectedRows = manufactureRecordModifyApplyManager.saveManufactureRecordModifyApply(manufactureRecordModifyApplyBO);
		if (affectedRows > 0) {
			int row = manufactureRecordManager.updateManufactureRecord(ManufactureRecordBO.builder()
					.manufactureRecordId(manufactureRecordModifyApplyBO.getManufactureRecordId())
					.status(ManufactureRecordConstants.STATUS_MODIFYING).build());
			if (row > 0) {
				return Result.success();
			}
		}
		return Result.ofErrorRowsAffected();
	}

	private void buildBO(ManufactureRecordModifyApplyBO manufactureRecordModifyApplyBO) {
		manufactureRecordModifyApplyBO.setCreateTime(LocalDateTime.now());
		manufactureRecordModifyApplyBO.setCreateUserId(UserContextHolder.getUserId());
		manufactureRecordModifyApplyBO.setDeleteFlag(false);
		manufactureRecordModifyApplyBO.setStatus(ManufactureRecordModifyApplyConstants.STATUS_UNMOTIFY);
		manufactureRecordModifyApplyBO.setTenantId(UserContextHolder.getTenantId());
		manufactureRecordModifyApplyBO.setUpdateTime(LocalDateTime.now());
	}

	@Override
	public Result<?> saveAllManufactureRecordModifyApply(Collection<ManufactureRecordModifyApplyBO> collection) {
		int affectedRows = manufactureRecordModifyApplyManager.saveAllManufactureRecordModifyApply(collection);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Transactional
	@Override
	public Result<?> updateManufactureRecordModifyApply(ManufactureRecordModifyApplyBO manufactureRecordModifyApplyBO) {
		int affectedRows = manufactureRecordModifyApplyManager.updateManufactureRecordModifyApply(manufactureRecordModifyApplyBO);
		if (affectedRows > 0) {
			ManufactureRecordModifyApplyBO manufactureRecordModifyApply = manufactureRecordModifyApplyManager.getManufactureRecordModifyApply(manufactureRecordModifyApplyBO.getManufactureRecordModifyApplyId());
			if (ManufactureRecordModifyApplyConstants.STATUS_MOTIFY==manufactureRecordModifyApplyBO.getStatus()) {
				int row = manufactureRecordManager.updateManufactureRecord(ManufactureRecordBO.builder()
						.manufactureRecordId(manufactureRecordModifyApply.getManufactureRecordId())
						.status(ManufactureRecordConstants.STATUS_RECORDING).build());
				if (row > 0) {
					return Result.success();
				}
			}
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> deleteManufactureRecordModifyApply(Long manufactureRecordModifyApplyId) {
		int affectedRows = manufactureRecordModifyApplyManager.deleteManufactureRecordModifyApply(manufactureRecordModifyApplyId);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public ManufactureRecordModifyApplyBO getManufactureRecordModifyApply(Long manufactureRecordModifyApplyId) {
		return manufactureRecordModifyApplyManager.getManufactureRecordModifyApply(manufactureRecordModifyApplyId);
	}

	@Override
	public List<ManufactureRecordModifyApplyBO> listManufactureRecordModifyApplyById(Collection<Long> collection) {
		return manufactureRecordModifyApplyManager.listManufactureRecordModifyApplyById(collection);
	}

	@Override
	public List<ManufactureRecordModifyApplyBO> listManufactureRecordModifyApply(ManufactureRecordModifyApplyQuery query) {
		List<ManufactureRecordModifyApplyBO> manufactureRecordModifyApplyBOS = manufactureRecordModifyApplyManager.listManufactureRecordModifyApply(query);
		if (CollectionUtils.isEmpty(manufactureRecordModifyApplyBOS)) {
			return manufactureRecordModifyApplyBOS;
		}
		List<Long> createUserIds = manufactureRecordModifyApplyBOS.stream().map(ManufactureRecordModifyApplyBO::getCreateUserId).collect(Collectors.toList());
		userClient.buildByUser(manufactureRecordModifyApplyBOS,createUserIds,ManufactureRecordModifyApplyBO::getCreateUserId,(bo,uto)->bo.setCreateUserName(uto.getUserName()));
		List<Long> recordIds = manufactureRecordModifyApplyBOS.stream().map(ManufactureRecordModifyApplyBO::getManufactureRecordId).collect(Collectors.toList());
		List<ManufactureRecordBO> manufactureRecordBOS = manufactureRecordManager.listManufactureRecordById(recordIds);
		dictClient.buildByDict(manufactureRecordBOS,"product",bo->bo.getProductDictCode(),(bo,value)->bo.setProductDictValue(value));
		dictClient.buildByDict(manufactureRecordBOS,"product",bo->bo.getSubProductDictCode(),(bo,value)->bo.setSubProductDictValue(value));
		Map<Long, ManufactureRecordModifyApplyBO> rid2MMap = manufactureRecordModifyApplyBOS.stream().collect(Collectors.toMap(ManufactureRecordModifyApplyBO::getManufactureRecordId, e -> e,(k1,k2)->k2));
		for (ManufactureRecordBO manufactureRecordBO : manufactureRecordBOS) {
			BeanUtils.copyProperties(manufactureRecordBO,rid2MMap.get(manufactureRecordBO.getManufactureRecordId()));
		}

		return manufactureRecordModifyApplyBOS;
	}

	@Override
	public int countManufactureRecordModifyApply(ManufactureRecordModifyApplyQuery query) {
		return manufactureRecordModifyApplyManager.countManufactureRecordModifyApply(query);
	}

}
